package org.example.seckilljava.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.example.seckilljava.config.KafkaConfig;
import org.example.seckilljava.dto.SeckillMessage;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class KafkaProducerService {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @Resource
    private ObjectMapper objectMapper;

    public void sendSeckillMessage(SeckillMessage message) {
        try {
            String messageJson = objectMapper.writeValueAsString(message);
            kafkaTemplate.send(KafkaConfig.SECKILL_TOPIC, message.getOrderNum(), messageJson);
            // log.info("发送秒杀消息成功: {}", messageJson);
        } catch (JsonProcessingException e) {
            log.error("发送秒杀消息失败", e);
        }
    }
} 